/*
 * CreateEditPayment.java
 * 
 * Created: Aug 4, 2015
 * 
 * Copyright (c) ExpertPB 2015
 * All information contained herein is, and remains the property of
 * ExpertPB and its suppliers, if any.
 */
package com.emex.app.stuff.supplier;

import com.emex.app.shared.data.SupplierMoney;
import com.emex.app.shared.data.enums.PayMethod;
import com.emex.app.stuff.utils.DateTimeService;
import com.emex.app.stuff.utils.UIUtils;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author Sergey
 */
public class CreateEditPayment extends javax.swing.JDialog {

    private SupplierMoney supplierMoney;
    private SupplierMoneyTableModel moneyTableModel;

    /**
     * Creates new form CreateEditPayment
     */
    public CreateEditPayment(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        datePicker.setDate(DateTimeService.getInstance().getDate());
        payMethodComboBox.setModel(new DefaultComboBoxModel(PayMethod.values()));
        jLabel3.setVisible(false);
        jLabel4.setVisible(false);
        jLabel5.setVisible(false);
        nklNumberTextField.setVisible(false);
        pathToFile.setVisible(false);
        payMethodComboBox.setVisible(false);
        pack();
        revalidate();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        summTextField = new javax.swing.JTextField();
        datePicker = new org.jdesktop.swingx.JXDatePicker();
        nklNumberTextField = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        pathToFile = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        payMethodComboBox = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(org.openide.util.NbBundle.getMessage(CreateEditPayment.class, "CreateEditPayment.title")); // NOI18N
        setResizable(false);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(CreateEditPayment.class, "CreateEditPayment.jLabel1.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(CreateEditPayment.class, "CreateEditPayment.jLabel2.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(CreateEditPayment.class, "CreateEditPayment.jLabel3.text")); // NOI18N

        summTextField.setText(org.openide.util.NbBundle.getMessage(CreateEditPayment.class, "CreateEditPayment.summTextField.text")); // NOI18N

        nklNumberTextField.setText(org.openide.util.NbBundle.getMessage(CreateEditPayment.class, "CreateEditPayment.nklNumberTextField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(saveButton, org.openide.util.NbBundle.getMessage(CreateEditPayment.class, "CreateEditPayment.saveButton.text")); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(CreateEditPayment.class, "CreateEditPayment.cancelButton.text")); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(CreateEditPayment.class, "CreateEditPayment.jLabel4.text")); // NOI18N

        pathToFile.setText(org.openide.util.NbBundle.getMessage(CreateEditPayment.class, "CreateEditPayment.pathToFile.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(CreateEditPayment.class, "CreateEditPayment.jLabel5.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 191, Short.MAX_VALUE)
                        .addComponent(saveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(summTextField)
                            .addComponent(datePicker, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(nklNumberTextField)
                            .addComponent(pathToFile)
                            .addComponent(payMethodComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(summTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(datePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(nklNumberTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(pathToFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(payMethodComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        if (null == datePicker.getDate() || summTextField.getText().isEmpty()) {
            UIUtils.showErrorMessage("Все поля обязательны для заполнения, кроме пути к файлу");
            return;
        }
        boolean isCreate = false;
        if (null == supplierMoney) {
            supplierMoney = new SupplierMoney();
            isCreate = true;
        }
        supplierMoney.setDate(datePicker.getDate());
        supplierMoney.setNaklNum(nklNumberTextField.getText());
        supplierMoney.setSumm(Double.valueOf(summTextField.getText().replaceAll(",", ".")));
        supplierMoney.setPathToFile(pathToFile.getText());
        supplierMoney.setPayMethod(payMethodComboBox.getSelectedIndex());
        if (isCreate) {
            moneyTableModel.dataAdded(supplierMoney);
        } else {
            moneyTableModel.dataUpdated();
        }
        setVisible(false);
    }//GEN-LAST:event_saveButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    public SupplierMoney getSupplierMoney() {
        return supplierMoney;
    }

    public void setSupplierMoney(SupplierMoney supplierMoney) {
        this.supplierMoney = supplierMoney;
        if (null != supplierMoney) {
            nklNumberTextField.setText(supplierMoney.getNaklNum());
            datePicker.setDate(supplierMoney.getDate());
            summTextField.setText(supplierMoney.getSumm().toString());
            pathToFile.setText(supplierMoney.getPathToFile());
            payMethodComboBox.setSelectedItem(supplierMoney.getPayMethod());
        }
    }

    public SupplierMoneyTableModel getMoneyTableModel() {
        return moneyTableModel;
    }

    public void setMoneyTableModel(SupplierMoneyTableModel moneyTableModel) {
        this.moneyTableModel = moneyTableModel;
    }

    public void supplierMode() {
        jLabel3.setVisible(true);
        jLabel4.setVisible(true);
        nklNumberTextField.setVisible(true);
        pathToFile.setVisible(true);
        pack();
        revalidate();
    }

    public void paymentByShopMode() {
        jLabel5.setVisible(true);
        payMethodComboBox.setVisible(true);
        pack();
        revalidate();
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private org.jdesktop.swingx.JXDatePicker datePicker;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField nklNumberTextField;
    private javax.swing.JTextField pathToFile;
    private javax.swing.JComboBox payMethodComboBox;
    private javax.swing.JButton saveButton;
    private javax.swing.JTextField summTextField;
    // End of variables declaration//GEN-END:variables
}
